<?php
/*******************************************************************
 * ORBHttpHandler.php
 * Copyright (C) 2006-2007 Midnight Coders, LLC
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is WebORB Presentation Server (R) for PHP.
 *
 * The Initial Developer of the Original Code is Midnight Coders, LLC.
 * All Rights Reserved.
 ********************************************************************/



if (!defined("WebOrb"))
{
    define("WebOrb", dirname(__FILE__) . DIRECTORY_SEPARATOR);
}

require_once(WebOrb . "Dispatch/Dispatchers.php");
require_once(WebOrb . "Message/Request.php");
require_once(WebOrb . "Config/ORBConfig.php");
require_once(WebOrb . "Util/Logging/Log.php");
require_once(WebOrb . "Util/Network.php");
require_once(WebOrb . "Writer/MessageWriter.php");
//require_once(WebOrb . "PHPErrorHandler.php");

final class ORBHttpHandler
{

    public function __construct()
    {
        session_start();
    }

    public function processRequest()
    {
        if($_SERVER["REQUEST_METHOD"] == "GET")
        {
          print("WebORB v2.0.2");
          //header( 'Location: ../Console/console.html' ) ;
          return;
        }


        $timeStart = microtime(true);

        ob_start();

        $inputData = file_get_contents("php://input");

        try
        {
            $config = ORBConfig::getInstance();

			if (!defined("WebOrbServicesPath"))
			{
				define("WebOrbServicesPath", realpath(WebOrb . $config->getServicePath() ) . DIRECTORY_SEPARATOR);
				Log::log(LoggingConstants::DEBUG, "WebORB services path is - ". WebOrbServicesPath );
			}

            $request = $config->getProtocolRegistry()->buildMessage(
                /*$GLOBALS["HTTP_CONTENT_TYPE"]*/"",
                $inputData); //
        }
        catch(Exception $e)
        {
           Log::logException(LoggingConstants::ERROR,"Internal error",$e);
           return;
        }


        if(Dispatchers::dispatch($request))
        {
            $this->serializeResponse($request);
        }

        $logMessage =  sprintf("Execute time: %0.3f", microtime(true) - $timeStart);

        Log::log(LoggingConstants::PERFORMANCE,$logMessage);
    }

    private function serializeResponse(Request $request)
    {
        $formatter = $request->getFormatter();

        MessageWriter::writeObject($request, $formatter);

        $byteBuffer = $formatter->getBytes();

        Log::log(LoggingConstants::INFO,  ob_get_contents());

        ob_clean();

        header("Content-type: " . $formatter->getContentType());
        header("Content-length: " . strlen($byteBuffer));

        $formatter->cleanup();

        print($byteBuffer);

        ob_end_flush();
    }

}

?>
